package
{
	import com.ghost.manager.Js_manager;
	import com.ghost.manager.Load_manager;
	import com.ghost.manager.Pop_win_manager;
	import com.ghost.manager.Socket_manager;
	import com.ghost.manager.System_set;
	import com.ghost.manager.UI_manager;
	import com.ghost.model.Configure_url;
	import com.ghost.model.Global_data;
	import com.ghost.model.Loading_data;
	import com.ghost.model.Main_start;
	import com.ghost.model.Object_pool;
	import com.ghost.utils.Debug;
	import com.ghost.utils.GC;
	import com.ghost.utils.MovieClipUtil;
	import com.ghost.view.Main.Main_mc;
	import com.ghost.view.UI.UI_main;
	import com.ghost.view.login.Login_main;
	import com.ghost.view.pop.Pop_main;
	import com.ghost.view.pop.Show_game_des;
	import com.ghost.vo.loading.Loading_list;
	
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.net.URLRequest;
	
	/**
	 * @author ghost
	 *主程序 
	 */
	[SWF(frameRate="30",width="1000",height="600",backgroundColor="0x000000")]
	public class architecture extends Sprite
	{
		private var game_main_mc:Sprite;//游戏主容器
		private var show_des_mc:Show_game_des;//文字提示或其他容器
		private var pop_mc:Pop_main;//存放pop容器
		private var bg_mc:Sprite;//最底层
		private var ui_mc:UI_main;//存放ui
		private var main_start:Main_start;//资源加载
		private var main_mc:Main_mc;//登录完成后的主界面(主要地图)
		private var login_main:Login_main;//登录
		public function architecture()
		{
			if(stage)add();
			else this.addEventListener(Event.ADDED_TO_STAGE,add);
		}
		private var o:Object;
		private function add():void
		{
			Global_data.Game_s = stage;
			Js_manager.getInstance().ori(Global_data.Game_s,1000,600,1440,900,1000,600);
			/*if(ExternalInterface.available)
			{
				o = ExternalInterface.call('return_w_h');
				get_w_h(o.w,o.h);
			}*/
			//this.addEventListener(MouseEvent.CLICK,screen_Handler);
			this.removeEventListener(Event.ADDED_TO_STAGE,add);
			Global_data.Game_s.addEventListener(Event.RESIZE,resize_Handler);
			System_set.getInstance().init(this);
			Object_pool.init();
			init_instance();
			game_main_mc.addEventListener('into_main',into);
		}
		private var i:int;
		private function screen_Handler(e:MouseEvent):void
		{
			i++;
			if(i%2)
			{
				Js_manager.getInstance().resize_Stage(1440,700);
			}
			else
			{
				Js_manager.getInstance().resize_Stage(Global_data.stage_r_width,Global_data.stage_r_height);
			}
		}
		public function get_w_h(w:*,h:*):void
		{
			Global_data.stage_s_width = w;
			Global_data.stage_s_height = h;
			Js_manager.getInstance().ori(Global_data.Game_s,w,h,1440,900,1000,600);
		}
		private function resize_Handler(e:Event):void
		{
			if(game_main_mc)
			{
				game_main_mc.x = (Global_data.stage_r_width-Global_data.stage_s_width)/2;
				game_main_mc.y = (Global_data.stage_r_height-Global_data.stage_s_height)/2;
				Debug.log(new Array(game_main_mc.x,game_main_mc.y));
				Pop_win_manager.getInstance().reflash();
				UI_manager.getInstance().update();
				show_des_mc.reflash();
			}
		}
		private function init_instance():void
		{
			main_start = new Main_start();
			main_start.mc = this;
			game_main_mc = new Sprite();//主容器
			this.addChild(game_main_mc);
			main_mc = new Main_mc();
			game_main_mc.addChild(main_mc);
			ui_mc = new UI_main();//ui容器
			game_main_mc.addChild(ui_mc);
			pop_mc = new Pop_main();//弹出窗口容器
			game_main_mc.addChild(pop_mc);
			Global_data.pop_main = pop_mc;
			show_des_mc = new Show_game_des();//文字提示或其他容器
			game_main_mc.addChild(show_des_mc);
			Global_data.show_game_des = show_des_mc;
			this.addEventListener("end",loading_end_Handler);
			var ld:Loader = new Loader();
			ld.load(new URLRequest(Configure_url.swf_url+"loading_ui.swf"),System_set.getInstance().cn);
			ld.contentLoaderInfo.addEventListener(Event.COMPLETE,cHandler);
		}
		private function loading_Handler(data:*):void
		{
			var loading_list:Loading_list = new Loading_list(XML(data));
			Global_data.loading_des_list = loading_list.list;
			main_start.load_source();
		}
		private function cHandler(e:Event):void
		{
			Load_manager.get_Instance().add(Configure_url.loading_xml_url,Loading_data.type_xml,"loading数据1",loading_Handler);
			Load_manager.get_Instance().start();
		}
		//数据加载完成
		private function loading_end_Handler(e:Event):void
		{
			if(Global_data.game_configure_list['login']=="1")
			{
				//添加登录界面
				if(!login_main)login_main = new Login_main();
				game_main_mc.addChild(login_main);
			}
			else
			{
				//直接进入游戏
				into();
			}
			if(Global_data.game_configure_list['connect']=="1")
			{
				//启用socket连接
				if(Global_data.connected_bool)return;
				else Socket_manager.getInstance().connSocket(Global_data.game_ip,Global_data.game_port,suc_Handler,fault_Handler)
			}
		}
		private function into(e:*=null):void
		{
			//MovieClipUtil.removeAllChild(game_main_mc);//移除主游戏容器的所有内容
			game_main_mc.removeChild(login_main);
			login_main = null;
			GC.nowGC();
			main_mc.first_init();//进入主界面地图系统 
		}
		//服务器连接成功
		private function suc_Handler():void
		{
			
		}
		//服务器连接失败
		private function fault_Handler():void
		{
			
		}
	}
}